package com.ahdy.controller;

import com.ahdy.entity.FamilyUser;
import com.ahdy.service.FamilyUserService;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 子女用户管理控制器
 */
@RestController
@RequestMapping("/api/family-users")
public class FamilyUserController {

    @Autowired
    private FamilyUserService familyUserService;

    /**
     * 添加子女用户
     * 
     * @param familyUser 子女用户实体
     * @return 响应结果
     */
    @PostMapping
    public ResponseEntity<Map<String, Object>> addFamilyUser(@Valid @RequestBody FamilyUser familyUser) {
        Integer id = familyUserService.addFamilyUser(familyUser);
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "添加子女用户成功");
        response.put("data", id);
        return ResponseEntity.status(HttpStatus.CREATED).body(response);
    }

    /**
     * 更新子女用户信息
     * 
     * @param id         用户ID
     * @param familyUser 子女用户实体
     * @return 响应结果
     */
    @PutMapping("/{id}")
    public ResponseEntity<Map<String, Object>> updateFamilyUser(@PathVariable Integer id,
            @Valid @RequestBody FamilyUser familyUser) {
        familyUser.setId(id);
        int rows = familyUserService.updateFamilyUser(familyUser);

        Map<String, Object> response = new HashMap<>();
        if (rows > 0) {
            response.put("code", 200);
            response.put("message", "更新子女用户信息成功");
        } else {
            response.put("code", 404);
            response.put("message", "子女用户不存在");
        }
        return ResponseEntity.ok(response);
    }

    /**
     * 删除子女用户
     * 
     * @param id 用户ID
     * @return 响应结果
     */
    @DeleteMapping("/{id}")
    public ResponseEntity<Map<String, Object>> deleteFamilyUser(@PathVariable Integer id) {
        int rows = familyUserService.deleteFamilyUser(id);

        Map<String, Object> response = new HashMap<>();
        if (rows > 0) {
            response.put("code", 200);
            response.put("message", "删除子女用户成功");
        } else {
            response.put("code", 404);
            response.put("message", "子女用户不存在");
        }
        return ResponseEntity.ok(response);
    }

    /**
     * 根据ID查询子女用户
     * 
     * @param id 用户ID
     * @return 响应结果
     */
    @GetMapping("/{id}")
    public ResponseEntity<Map<String, Object>> getFamilyUserById(@PathVariable Integer id) {
        FamilyUser familyUser = familyUserService.getFamilyUserById(id);

        Map<String, Object> response = new HashMap<>();
        if (familyUser != null) {
            response.put("code", 200);
            response.put("message", "获取子女用户信息成功");
            response.put("data", familyUser);
            return ResponseEntity.ok(response);
        } else {
            response.put("code", 404);
            response.put("message", "子女用户不存在");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 查询所有子女用户
     * 
     * @return 响应结果
     */
    @GetMapping
    public ResponseEntity<Map<String, Object>> getAllFamilyUsers() {
        List<FamilyUser> familyUsers = familyUserService.getAllFamilyUsers();

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取子女用户列表成功");
        response.put("data", familyUsers);
        return ResponseEntity.ok(response);
    }

    /**
     * 根据手机号查询子女用户
     * 
     * @param phone 手机号
     * @return 响应结果
     */
    @GetMapping("/by-phone/{phone}")
    public ResponseEntity<Map<String, Object>> getFamilyUserByPhone(@PathVariable String phone) {
        FamilyUser familyUser = familyUserService.getFamilyUserByPhone(phone);

        Map<String, Object> response = new HashMap<>();
        if (familyUser != null) {
            response.put("code", 200);
            response.put("message", "获取子女用户信息成功");
            response.put("data", familyUser);
            return ResponseEntity.ok(response);
        } else {
            response.put("code", 404);
            response.put("message", "子女用户不存在");
            return ResponseEntity.status(HttpStatus.NOT_FOUND).body(response);
        }
    }

    /**
     * 用户登录
     * 
     * @param loginParams 登录参数
     * @return 响应结果
     */
    @PostMapping("/login")
    public ResponseEntity<Map<String, Object>> login(@RequestBody Map<String, String> loginParams) {
        String phone = loginParams.get("phone");
        String password = loginParams.get("password");

        FamilyUser familyUser = familyUserService.login(phone, password);

        Map<String, Object> response = new HashMap<>();
        if (familyUser != null) {
            response.put("code", 200);
            response.put("message", "登录成功");
            response.put("data", familyUser);
            return ResponseEntity.ok(response);
        } else {
            response.put("code", 401);
            response.put("message", "手机号或密码错误");
            return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(response);
        }
    }

    /**
     * 根据老人ID查询关联的子女列表
     * 
     * @param elderlyId 老人用户ID
     * @return 响应结果
     */
    @GetMapping("/by-elderly/{elderlyId}")
    public ResponseEntity<Map<String, Object>> getFamilyUsersByElderlyId(@PathVariable Integer elderlyId) {
        List<FamilyUser> familyUsers = familyUserService.getFamilyUsersByElderlyId(elderlyId);

        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "获取关联子女列表成功");
        response.put("data", familyUsers);
        return ResponseEntity.ok(response);
    }
}